package com.ruoyi.project.hbase;

import com.google.common.collect.Lists;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HBaseUtil {
/*
    *//**
     * 创建HBase表.
     *
     * @param tableName 表名
     * @param cfs       列族的数组
     * @return 是否创建成功
     *//*
    public static boolean createTable(String tableName, List<String> cfs) {
        try (HBaseAdmin admin = (HBaseAdmin) HBaseConn.getHBaseConn().getAdmin()) {//获取操作对象admin
            if (admin.tableExists(TableName.valueOf(tableName))) {
                return false;
            }
            List<ColumnFamilyDescriptor> familyDescriptors = new ArrayList<>(cfs.size());
            for (String column : cfs) {
                familyDescriptors.add(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(column)).build());
            }
            TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName))
                    .setColumnFamilies(familyDescriptors).build();
            admin.createTable(tableDescriptor);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    *//**
     * 查看HBase表是否存在
     *
     * @param tableName 表名
     * @return 是否存在该表
     *//*
    public static boolean isTableExist(String tableName) {
        try (HBaseAdmin admin = (HBaseAdmin) HBaseConn.getHBaseConn().getAdmin()) {
            if (admin.tableExists(TableName.valueOf(tableName))) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    *//**
     * 删除hbase表.
     *
     * @param tableName 表名
     * @return 是否删除成功
     *//*
    public static boolean deleteTable(String tableName) {
        try (HBaseAdmin admin = (HBaseAdmin) HBaseConn.getHBaseConn().getAdmin()) {
            admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    *//**
     * hbase插入一条数据.
     *put参数 family列族名  column列名本身 val/data 列值
     * String family="info" string column= "name" string val = "zhangsan"
     * @param tableName 表名
     * @param rowKey    唯一标识
     * @param cfName    列族名（family)
     * @param qualifier 列标识 列名本身
     * @param data      数据 列值
     * @return 是否插入成功
     *//*
    public static boolean putRow(String tableName, String rowKey, String cfName, String qualifier,
                                 String data) {
        try (Table table = HBaseConn.getTable(tableName)) {
            Put put = new Put(Bytes.toBytes(rowKey));//把字符串变成字节序列
            put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier), Bytes.toBytes(data));//增加列
            table.put(put);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return true;
    }

    public static boolean putRows(String tableName, List<Put> puts) {
        try (Table table = HBaseConn.getTable(tableName)) {
            table.put(puts);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return true;
    }

    public static void main(String[] args) {
        List<String> cfs = Lists.newArrayList();
        cfs.add("data");
        HBaseUtil.createTable("test",cfs);
    }*/
}